package de.kardass.jsmatep.in;

import de.kardass.jsmatep.in.config.ParserConfiguration;
import de.kardass.jsmatep.in.config.field.Field;

public class FieldParsingException extends Exception {

	private static final long serialVersionUID = -8531186733455286777L;
	
	/** The raw problem record */
	private final String rawRecord;

	/** The field the error occurred*/
    private final Field<?> field;

    /** The raw field value (string representation) the error occurred*/
    private final String rawFieldValue;

    /** the underlying parser configuration */
    private final ParserConfiguration parserConfiguration;

    /** factory for building dynamic value object from the raw record */
    private DynamicValueObjectFactory dynamicValueObjectFactory;
	

	
	public FieldParsingException(String rawRecord, String message, 
	        Field<?> field, String rawFieldValue, ParserConfiguration parserConfiguration) {
		super(message);
		this.rawRecord = rawRecord;
		this.field = field;
		this.rawFieldValue = rawFieldValue;
		this.parserConfiguration = parserConfiguration;
	}

    /**
     * {@link #rawRecord}
     * @return
     * @since 18.05.2010
     */
	public String getRawRecord() {
		return rawRecord;
	}

	
	@Override
	public String getMessage() {
		return "Error while parsing field: [" + rawFieldValue + "] " + super.getMessage();
	}
	
	@Override
	public String toString() {
		return "Error while parsing field: [" + rawFieldValue + "] " + super.toString();
	}

    /**
     * {@link #field}
     * @return
     * @since 18.05.2010
     */
    public Field<?> getField() {
        return field;
    }

    /**
     * {@link #rawFieldValue}
     * @return
     * @since 18.05.2010
     */
    public String getRawFieldValue() {
        return rawFieldValue;
    }

    public ParserConfiguration getParserConfiguration() {
        return parserConfiguration;
    }

    public DynamicValueObject recordAsDynamicValueObject() {
        return getDynamicValueObjectFactory().asDynamicValueObject(rawRecord);
    }

    private DynamicValueObjectFactory getDynamicValueObjectFactory() {
        if (dynamicValueObjectFactory == null) {
            dynamicValueObjectFactory = new DynamicValueObjectFactory(parserConfiguration);
        }
        return dynamicValueObjectFactory;
    }
    

}
